package com.xxx.video.commen.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.xxx.video.commen.utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecycleViewAdapter<V, VH extends BaseViewHolder> extends RecyclerView.Adapter<VH> {
    /**
     * data
     */
    public List<V> data;
    /**
     * item click call back
     */
    public OnItemClickListener<V> onItemClickListener;
    /**
     * context
     */
    public Context context;


    public BaseRecycleViewAdapter(@NonNull Context context) {
        this.context = context;
    }

    /**
     * is need item click
     */
    public boolean isSetBaseItemViewClickListener() {
        return true;
    }

    /**
     * set data
     *
     * @param data data
     */
    public void setData(List<V> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    /**
     * add data
     *
     * @param datas data
     */
    public void addData(List<V> datas) {
        if (ListUtils.isEmpty(data)) {
            data = new ArrayList<>();
        }
        if (!ListUtils.isEmpty(datas)) {
            this.data.addAll(datas);
            notifyDataSetChanged();
        }
    }

    /**
     * add data
     *
     * @param datas    data
     * @param position position
     */
    public void addData(List<V> datas, int position) {
        if (ListUtils.isEmpty(data)) {
            data = new ArrayList<>();
        }
        if (!ListUtils.isEmpty(datas)) {
            this.data.addAll(position, datas);
            notifyDataSetChanged();
        }
    }

    /**
     * add by position
     *
     * @param da       data
     * @param position position
     */
    public void addData(V da, int position) {
        if (da != null) {
            if (ListUtils.isEmpty(this.data)) {
                data = new ArrayList<>();
            }
            data.add(position, da);
            notifyDataSetChanged();
        }
    }

    /**
     * add data last
     *
     * @param da data
     */
    public void addData(V da) {
        if (da != null) {
            if (ListUtils.isEmpty(this.data)) {
                data = new ArrayList<>();
            }
            data.add(da);
            notifyDataSetChanged();
        }
    }

    /**
     * remove data
     *
     * @param da data
     */
    public void removeData(V da) {
        if (da != null && !ListUtils.isEmpty(this.data)) {
            data.remove(da);
            notifyDataSetChanged();
        }
    }

    /**
     * remove data by position
     *
     * @param position position
     */
    public void removeDataByPosition(int position) {
        if (!ListUtils.isEmpty(this.data)) {
            data.remove(position);
            notifyDataSetChanged();
        }
    }

    /**
     * remove all data
     */
    public void removeAllData() {
        if (!ListUtils.isEmpty(this.data)) {
            data.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        if (ListUtils.isEmpty(data)) {
            return 0;
        }
        return data.size();
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        holder.setPosition(position);
        holder.setData2View(position);
        if (holder.itemView != null && isSetBaseItemViewClickListener()) {
            holder.itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(getItem(position), position, 0);
                }
            });
        }
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(getItemLayout(viewType), parent, false);
        return onCreateDefaultViewHolder(view, viewType);
    }

    /**
     * create  holder
     *
     * @param view     view
     * @param viewType vuew type
     * @return return holder
     */
    public abstract VH onCreateDefaultViewHolder(View view, int viewType);

    /**
     * get item layout
     *
     * @param viewType view type
     * @return view
     */
    public abstract int getItemLayout(int viewType);


    public interface OnItemClickListener<V> {
        /**
         * item click method
         *
         * @param v        item data
         * @param position position
         * @param type     type
         */
        void onItemClick(V v, int position, int type);
    }

    /**
     * get item data
     *
     * @param position position
     * @return data
     */
    public V getItem(int position) {
        return data.get(position);
    }

    /**
     * set item click listener
     *
     * @param onItemClickListener call back
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public List<V> getData() {
        return data;
    }
}
